var express = require('express');
var router = express.Router();

const GradeModel = require('../db/GradeModel');
const Fun = require("../middlewares/Fun")
const moment = require('moment');
const now = () => {
    return moment().format('YYYY-MM-DD HH:mm:ss')
}
/***
 *  list 会员等级列表
 *  one 查找会员等级
 *  save 保存会员等级
 *  update 更新会议等级
 *  delete 删除会员等级
 */

router.post('/hadnamed', function (req, res, next) {
    if (!req.body.name) {
        return res.json({ code: 201, mes: '缺少必填信息！' });
    }

    GradeModel.findOne({ name: req.body.name }).then((data) => {
        if (data) {
            return res.json({ code: 201, mes: '该名称已经存在！' });
        } else {
            return res.json({ code: 200, mes: '该名称可用！' });
        }
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' })
    });

});

// 保存门店

router.post('/save', function (req, res, next) {
    // 获取条件
    const gread = new GradeModel(req.body);
    let user = req.user

    //
    GradeModel.find({ name: req.body.name }).then((data) => {
        if (data.length != 0) { return res.json({ code: 201, mes: '会员等级名称已经存在' }); }
        else {
            gread.save().then(() => {
                Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "新增会员类型成功");
                return res.json({ code: 200, mes: '来源创建成功' })
            }).catch(() => {
                Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "新增会员类型失败");
                return res.json({ code: 201, mes: '发生意外错误！' })
            })
        }
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' })
    });
});
// 更新来源信息
router.post('/update', function (req, res, next) {
    // 更改密码的判断需要额外处理
    const set = req.body;
    const id = req.body.id;
    GradeModel.findById(id).then(
        (data) => {
            if (data == null) { return res.json({ code: 201, mes: '传入id错误' }); }
            else {
                return GradeModel.update({ _id: id }, { $set: set }).then(
                    () => { return res.json({ code: 200, mes: '更改成功' }); }
                ).catch(() => {
                    return res.json({ code: 201, mes: '更改失败！' })
                });
            }
        }
    ).catch(function () {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
// 查找所有会员等级
router.get('/list', function (req, res, next) {
    GradeModel.find().then((gread) => {
        return res.json({ code: 200, gread });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});
// 查找指定来源
router.get('/one/:id', function (req, res, next) {
    const id = req.params.id;
    GradeModel.findById(id).then((gread) => {
        return res.json({ code: 200, gread });
    }).catch(() => {
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});

// 删除等级接口
router.post('/delete', function (req, res, next) {
    let user = req.user
    const id = req.body.id;
    GradeModel.remove({ _id: id }).then(() => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "删除会员类型成功");
        return res.json({ code: 200, mes: '删除成功！' });
    }).catch(() => {
        Fun.saveLogs(user, req.ip.replace(/::ffff:/, ''), "删除会员类型失败");
        return res.json({ code: 201, mes: '发生意外错误！' });
    });
});


module.exports = router;
